﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Text;

using PdfCreative.Pdf.Graphics;

namespace Stars
{
    class Program
    {
        static void Main(string[] args)
        {
            // We will create 'HelloWorld.pdf' in the user's temporary folder.
            string path = Path.Combine(Path.GetTempPath(), "Stars.pdf");

            // Create a new Graphics instance.
            Graphics g = new Graphics(path);

            // Render the text.
            RenderStars(g);

            // Close the graphics.
            g.Close();

            // Open the PDF document.
            Process.Start(path);
        }

        /// <summary>
        /// Render stars in a circle, demonstrating shapes, gradients, stencils and
        /// transformations.
        /// </summary>
        /// <param name="g"></param>
        private static void RenderStars(Graphics g)
        {
            g.DrawString(
                Rgb.BlackBrush,
                "Sample project: Star",
                new Font("Times New Roman Bold"),
                10f,
                36f,
                36f);

            g.DrawString(
                Rgb.BlackBrush,
                "Render stars in a circle, demonstrating shapes, " +
                "gradients, stencils and transformations.",
                new Font("Times New Roman"),
                10f,
                36f,
                50f);

            Stencil star = CreateStar(g);

            Stencil ring = CreateRing(g, star);

            g.DrawStencil(ring, 100, 100);

        }

        /// <summary>
        /// Creates a reusable graphic in the shape of a
        /// ring of stars.
        /// </summary>
        /// <param name="g">The Graphics instance.</param>
        /// <param name="star">The star stencil.</param>
        /// <returns>The ring stencil.</returns>
        private static Stencil CreateRing(Graphics g, Stencil star)
        {
            // Create a new stencil that is composed of a ring of stars.
            Stencil ring = g.CreateStencil(400, 400);

            ring.Graphics.TransformTranslate(200, 200);
            double increment = Math.PI / 6f;
            for (double angle = 0; angle < Math.PI * 2; angle += increment)
            {
                ring.Graphics.DrawStencil(
                    star,
                    (float)Math.Cos(angle) * 150f,
                    (float)Math.Sin(angle) * 150);
            }

            return ring;
        }

        /// <summary>
        /// Creates a reusable graphic in the shape of a star.
        /// </summary>
        /// <param name="g">The graphics instance.</param>
        /// <returns>The star stencil.</returns>
        private static Stencil CreateStar(Graphics g)
        {
            // Define a path in the shape of a star.
            GraphicsPath path = new GraphicsPath();
            path.AddLine(0.0f, 0.0f, 6.0f, 12.0f);
            path.AddLine(6.0f, 12.0f, 19.0f, 13.8f);
            path.AddLine(19.0f, 13.8f, 9.7f, 23.2f);
            path.AddLine(9.7f, 23.2f, 12.0f, 36.0f);
            path.AddLine(12.0f, 36.0f, 0.0f, 30.0f);
            path.AddLine(0.0f, 30.0f, -11.6f, 36.4f);
            path.AddLine(-11.6f, 36.4f, -9.4f, 23.0f);
            path.AddLine(-9.4f, 23.0f, -19.0f, 13.8f);
            path.AddLine(-19.0f, 13.8f, -6.0f, 12.0f);
            path.Close();

            // Create a new stencil - a reusable graphic.
            Stencil star = g.CreateStencil(40, 40);

            // Create a radial gradient to fill the star with.
            Brush gradient = new RadialGradientBrush(
                Rgb.White, Rgb.Yellow, 
                new Rectangle(-20, 0, 40, 40), 0, 0.3f);

            // Fill the star and draw its outline.
            star.Graphics.FillPath(gradient, path, 20, 0);
            star.Graphics.DrawPath(Rgb.RedPen, path, 20, 0);

            return star;

        }
    }
}
